#include "mainwindow.h"
#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),ui(new Ui::MainWindow),x(0),y(0),
      z(0),size(1050,750),FileIndex(0)
{

    ui->setupUi(this);
    this->setSizeIncrement(size);
    initToolBar();
    ui->stackedWidget->setCurrentWidget(ui->HomePage);
    connect(HomeBtn,&QAction::triggered,this,&MainWindow::TurntoHome);
    connect(LeftBtn,&QAction::triggered,this,&MainWindow::TurntoLeft);
    connect(RightBtn,&QAction::triggered,this,&MainWindow::TurntoRight);
    connect(ArrowAcn,&QAction::triggered,this,&MainWindow::ArrowAcn_triggered);
    connect(FourArrowAcn,&QAction::triggered,this,&MainWindow::FourArrowAcn_triggered);
    connect(AdjustAcn,&QToolButton::triggered,this,&MainWindow::AdjustAcn_triggered);
    connect(ZoomAcn,&QAction::triggered,this,&MainWindow::ZoomAcn_triggered);
    connect(SaveBtn,&QAction::triggered,this,&MainWindow::SaveFile);
    connect(ZoomIn,&QAction::triggered,this,&MainWindow::ZoomIn_triggered);
    connect(ZoomOut,&QAction::triggered,this,&MainWindow::ZoomOut_triggered);
}


MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initToolBar()
{
    HomeBtn = new QAction;
    HomeBtn->setIcon(QIcon(":/icon/home.png"));
    LeftBtn = new QAction;
    LeftBtn->setIcon(QIcon(":/icon/left.png"));
    RightBtn = new QAction;
    RightBtn->setIcon(QIcon(":/icon/right.png"));
    ArrowAcn = new QAction;
    ArrowAcn->setIcon(QIcon(":/icon/bluearrow.png"));
    FourArrowAcn = new QAction;
    FourArrowAcn->setIcon(QIcon(":/icon/fourarrow.png"));
    ZoomAcn = new QAction;
    ZoomAcn->setIcon(QIcon(":/icon/zoom.png"));


    AdjustAcn = new Adjust;

    SaveBtn = new QAction;
    SaveBtn->setIcon(QIcon(":/icon/save.png"));
    label = new QLabel;
    spacer = new QWidget;
    spacer -> setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    QString x_y_coordinate = QString("x= %1 , y= %2 , z= %3 ").arg(x).arg(y).arg(z);
    label->setText(x_y_coordinate);

    ui->toolBar->addAction(HomeBtn);
    ui->toolBar->addAction(LeftBtn);
    ui->toolBar->addAction(RightBtn);
    ui->toolBar->addSeparator();
    ui->toolBar->addAction(ArrowAcn);
    ui->toolBar->addAction(FourArrowAcn);



    //关于变大变小按钮的下拉菜单栏的设置
    Zoommenu = new QMenu(this);
    ZoomIn = new QAction("放大");
    Zoommenu->addAction(ZoomIn);
    ZoomIn->setIcon(QIcon(":/icon/zoomin.png"));
    ZoomIn->setIconVisibleInMenu(true);
    ZoomOut = new QAction("缩小");
    Zoommenu->addAction(ZoomOut);
    ZoomOut->setIcon(QIcon(":/icon/zoomout.png"));
    ZoomOut->setIconVisibleInMenu(true);
    Zoommenu->setWindowFlags(Zoommenu->windowFlags() | Qt::FramelessWindowHint);
    Zoommenu->setBaseSize(100,100);
    Zoommenu->move(1000,750);

    ZoomAcn->setMenu(Zoommenu);
    ui->toolBar->addAction(ZoomAcn);

    //调整的按钮添加
    ui->toolBar->addWidget(AdjustAcn);

    //添加保存
    ui->toolBar->addSeparator();
    ui->toolBar->addAction(SaveBtn);
    ui->toolBar->addWidget(spacer);
    ui->toolBar->addWidget(label);

    LeftBtn->setEnabled(false);
    RightBtn->setEnabled(false);
    ArrowAcn->setEnabled(false);
    FourArrowAcn->setEnabled(false);
    ZoomAcn->setEnabled(false);
    AdjustAcn->setEnabled(false);
    SaveBtn->setEnabled(false);
    label->setVisible(false);

}

void MainWindow::ShowFile()
{
    QPixmap ShownFile = QPixmap(OpenFileNames[FileIndex]);
    ShownFile.scaled(ui->centerview->size(),Qt::KeepAspectRatio,Qt::SmoothTransformation);
    centerscene.addPixmap(ShownFile);
}

void MainWindow::TurntoLeft()
{
    setCursor(Qt::ArrowCursor);
    RightBtn->setEnabled(true);
    centerscene.clear();
    FileIndex-=1;
    ShowFile();
    if(FileIndex==0){
        LeftBtn->setEnabled(false);
    }

}

void MainWindow::TurntoRight()
{
    setCursor(Qt::ArrowCursor);
    LeftBtn->setEnabled(true);
    centerscene.clear();
    FileIndex+=1;
    ShowFile();
    if(FileIndex==FileCnt-1){
        RightBtn->setEnabled(false);
    }
}

void MainWindow::FourArrowAcn_triggered()
{
    ZoomAcn->setIcon(QIcon(":/icon/zoom.png"));
    ui->centralwidget->setCursor(Qt::SizeAllCursor);
}

void MainWindow::ArrowAcn_triggered()
{
    ui->centralwidget->setCursor(Qt::ArrowCursor);
    ZoomAcn->setIcon(QIcon(":/icon/zoom.png"));
}

void MainWindow::ZoomAcn_triggered()
{
    ui->centralwidget->setCursor(Qt::ArrowCursor);
    if(ZoomAcn->isChecked()){Zoommenu->show();}
   ZoomAcn->setIcon(QIcon(":/icon/zoom.png"));

}

void MainWindow::AdjustAcn_triggered()
{
    ui->centralwidget->setCursor(Qt::ArrowCursor);
    ZoomAcn->setIcon(QIcon(":/icon/zoom.png"));
}



void MainWindow::SaveFile()
{
    ui->centralwidget->setCursor(Qt::ArrowCursor);
    ZoomAcn->setIcon(QIcon(":/icon/zoom.png"));
    QString File = QFileDialog::getSaveFileName(this,"保存文件","C:\\Users\\desktop");
    //还差一些保存3D文件的操作
    QMessageBox::information(this,"保存文件","保存文件成功",QMessageBox::Ok);

}

void MainWindow::TurntoHome()
{
    ui->centralwidget->setCursor(Qt::ArrowCursor);
    LeftBtn->setEnabled(false);
    RightBtn->setEnabled(false);
    ArrowAcn->setEnabled(false);
    FourArrowAcn->setEnabled(false);
    ZoomAcn->setEnabled(false);
    AdjustAcn->setEnabled(false);
    SaveBtn->setEnabled(false);
    label->setVisible(false);
    ui->stackedWidget->setCurrentWidget(ui->HomePage);
}

void MainWindow::ZoomIn_triggered()
{
    ZoomAcn->setIcon(QIcon(":/icon/zoomin.png"));
    QCursor Cursor= QCursor(QPixmap(":/icon/zoomin.png").scaled(40, 40, Qt::KeepAspectRatio));
    ui->centralwidget->setCursor(Cursor);
}

void MainWindow::ZoomOut_triggered()
{

    ZoomAcn->setIcon(QIcon(":/icon/zoomout.png"));
    QCursor Cursor= QCursor(QPixmap(":/icon/zoomout.png").scaled(40, 40, Qt::KeepAspectRatio));
    ui->centralwidget->setCursor(Cursor);
}
void MainWindow::on_ChooseBtn_clicked()
{
    OpenFileNames = QFileDialog::getOpenFileNames(this,"选择文件","C:\\Users\\desktop");
    FileCnt = OpenFileNames.count();
    ui->FileNameLabel->setText(QString("已选择%1个文件").arg(FileCnt));

}


void MainWindow::on_ConfirmBtn_clicked()
{
    if(FileCnt!=0){
        ui->centerview->setScene(&centerscene);
        FileIndex = 0;
        ShowFile();
        if(FileIndex!=FileCnt-1){
            RightBtn->setEnabled(true);
        }
        ArrowAcn->setEnabled(true);
        FourArrowAcn->setEnabled(true);
        ZoomAcn->setEnabled(true);
        AdjustAcn->setEnabled(true);
        SaveBtn->setEnabled(true);
        label->setVisible(true);

        ui->stackedWidget->setCurrentWidget(ui->DisplayPage);}
    else{QMessageBox::information(NULL,"无法打开","请选择文件！",QMessageBox::Ok);}
}

